﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;

using groupAnalyzer.Observers;
using groupAnalyzer.model;
using groupAnalyzer.controller;

namespace groupAnalyzer
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window, ObserverClient
    {
        private ViewFactory viewFactory;
        private Factory factory;

        public void notify(Messages message, Object payLoad)
        {
            switch (message)
            {
                case Messages.finishedBuildTree:
                   // List< root = this.viewFactory.Tree;
                    //new AdsTree(null).ShowDialog();
                    break;


                case Messages.analyzed:
                    this.viewFactory.copyNodes();

                    this.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(this.refreshBinding));
                    break;

                case Messages.error:
                    string errorToShow = "An unknown error occured";
                    if (payLoad != null)
                    {
                        errorToShow = payLoad.ToString();
                    }

                    MessageBox.Show(errorToShow);
                    break;
                default:
                    // do nothing
                    break;
            }

        }

        private void refreshBinding()
        {
            //this.treeview.Items
            this.treeview.ItemsSource = this.viewFactory.getNodes();
        }

        public static void init(model.Factory factory)
        {
            App app = new App();
            MainWindow window1 = new MainWindow(factory);
            
            Observer.getGlobalObserver().register(window1);
            app.MainWindow = window1;
            app.MainWindow.Show();
            app.Run();
        }

        public MainWindow(model.Factory factory)
        {
            this.viewFactory = new ViewFactory(factory);

            this.factory = factory;
            InitializeComponent();

        }

        private void exit_Click(object sender, RoutedEventArgs e)
        {
            Observer observer = Observer.getGlobalObserver();
            observer.notifiy(Messages.exit, null);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Observer.getGlobalObserver().notifiy(Messages.exit, null);
        }

        private void analyze_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void membership_click(object sender, ContextMenuEventArgs e)
        {
            /*   UserNodeView selectedNode = (UserNodeView)this.treeview.SelectedItem;
               Observer.getGlobalObserver().notifiy(Messages.getUsers, selectedNode.FQDN);*/
        }

        private void settings_Clicked(object sender, RoutedEventArgs e)
        {
            //Observer.getGlobalObserver().notifiy(Messages.saveSettings, null);
        }

        private void choose_group_Click(object sender, RoutedEventArgs e)
        {
            Observer searchObserver = new Observer();
            SearchController.init(searchObserver, this.factory);
            Searcher.init(this.viewFactory,searchObserver);
        }

        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            Observer.getGlobalObserver().notifiy(Messages.refresh, null);
        }

        private void analyzeAllMember_Click(object sender, RoutedEventArgs e)
        {
            //  "member", "memberof
            Observer.getGlobalObserver().notifiy(Messages.analyzeAll, "member");
        }

        private void analyzeAllMemberOf_Click(object sender, RoutedEventArgs e)
        {
            Observer.getGlobalObserver().notifiy(Messages.analyzeAll, "memberof");
        }

        private void editSettings_Click(object sender, RoutedEventArgs e)
        {
            Observer observer = new Observer();
            //SearchController.init(observer, this.factory);
            SettingsController.init(observer);
            SettingsWindow.init(this.viewFactory,observer);
           
        }


    }
}
